package edu.dali.enterprise_test.controller.login;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @Autowired
    private HttpServletRequest request;

    @GetMapping("/login")
    public String login() {
        return "login/login";
    }

    @PostMapping("/login")
    public String login(Model model) {//登录失败从request对象中可以获取shiro处理异常的细信息.
        //shiroLoginFailure.
        String exception = (String)request.getAttribute("shiroLoginFailure");
        System.out.println("exception="+exception);
        String msg = "";
        if(exception != null){
            if(UnknownAccountException.class.getName().equals(exception)){
                msg = "账号不存在";
            }else if(IncorrectCredentialsException.class.getName().equals(exception)){
                msg = "密码不正确";
            }else{
                msg = "else -->"+exception;
            }
        }
        model.addAttribute("msg", msg);


        return "login/login";
    }
}
